package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/4 16:47
 * @description
 */
public class Code107 {
    private List<List<Integer>> result = new ArrayList<>();
    private int maxDeep;

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        maxDeep = maxDepth(root);
        for (int i = 0; i < maxDeep; i++) {
            result.add(new ArrayList<>());
        }
        setResult(root, 0);
        return result;
    }

    public void setResult(TreeNode node, int deep) {
        if (node != null) {
            deep++;
            result.get(maxDeep - deep).add(node.val);
            setResult(node.left, deep);
            setResult(node.right, deep);
        }
    }

    public int maxDepth(TreeNode root) {
        return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }
}
